package com.hmis.hmis.UserInterface.diagnoseUI;

import com.hmis.dao.MedicineDAO;
import com.hmis.dao.PatientDAO;
import com.hmis.dao.PresciptionMedicineDAO;
import com.hmis.dao.PrescriptionDAO;
import com.hmis.domain.Medicine;
import com.hmis.domain.Patient;
import com.hmis.domain.Prescription;
import com.hmis.domain.PrescriptionMedicine;
import com.hmis.domain.dto.PrescriptionDto;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DiagnoseController {
    public TextField patientIdFiled;
    public TextArea patientInfoArea;
    public TextArea diagnoseArea;
    public TextArea prescriptionInfoArea;
    public Button diagnoseButton;
    public TextField medicineNameFiled;
    public TextArea medicineArea;
    public Button findMedicineButton;
    public Button addMedicineButton;
    public TextField medicineNumberFiled;
    public Button findPatientButton;
    public TextField dosageField;
    public ComboBox<String> medicineCombo;
    PatientDAO patientDAO = PatientDAO.patientDAO;
    PrescriptionDAO prescriptionDAO = PrescriptionDAO.prescriptionDAO;
    MedicineDAO medicineDAO = MedicineDAO.medicineDAO;
    PresciptionMedicineDAO prescriptionMedicineDAO = PresciptionMedicineDAO.presciptionMedicineDAO;
    //patient静态
    public static Patient patient_st = new Patient();
    public static Medicine medicine_st = new Medicine();
    public static Prescription prescription_st = new Prescription();
    public static PrescriptionDto prescriptionDto_st = new PrescriptionDto();
    public static List<Medicine> medicines_st = new ArrayList<>();

    public void diagnose(ActionEvent actionEvent) {
        patientInfoArea.clear();
        String diagnose = diagnoseArea.getText();
        patient_st.setDiagnose(diagnose);
        patientDAO.updatePatient(patient_st);
        patient_st = patientDAO.findById(patient_st.getPatientId());
        String patientInfo = "病历号：" + patient_st.getPatientId() +
                "\n病人姓名：" + patient_st.getPatientName() +
                "\n性别：" + patient_st.getSex() +
                "\n病房号：" + patient_st.getWardNo() +
                "\n床号：" + patient_st.getTheBedNo() +
                "\n病情：" + patient_st.getIllness() +
                "\n诊断：" + patient_st.getDiagnose();
        patientInfoArea.setText(patientInfo);
    }

    public void findMedicine(ActionEvent actionEvent) {
        String medicineName = medicineNameFiled.getText();
        List<Medicine> medicines = medicineDAO.getLikeAll(medicineName);
        if(medicines == null){
            medicineArea.setText("无法查找到该药品，请重新查找");
            return;
        }
        medicineCombo.getItems().clear();
        for(Medicine medicine : medicines){
            medicineCombo.getItems().add(medicine.getMedicineName());
        }
        medicines_st = medicines;
        medicineArea.clear();
    }

    public void addMedicine(ActionEvent actionEvent) {
        Integer medicineNumber = Integer.valueOf(medicineNumberFiled.getText());
        PrescriptionMedicine prescriptionMedicine = new PrescriptionMedicine();
        prescriptionMedicine.setMedicineId(medicine_st.getMedicineId());
        prescriptionMedicine.setMedicine(medicine_st.getMedicineName());
        prescriptionMedicine.setCopies(medicineNumber);
        prescriptionMedicine.setPrescriptionId(prescription_st.getPrescriptionId());
        double price = medicineNumber * medicine_st.getPrice().doubleValue();
        prescriptionMedicine.setTotalPrice(new BigDecimal(price));
        String dosage = dosageField.getText();
        prescriptionMedicine.setDosage(dosage);
        prescriptionMedicineDAO.save(prescriptionMedicine);
        prescription_st.setTotalPrice(prescription_st.getTotalPrice().add(new BigDecimal(price)));
        prescriptionDAO.updatePrescription(prescription_st);
        prescriptionDto_st.addPrescriptionMedicine(prescriptionMedicine);
        prescriptionInfoArea.clear();
        prescriptionInfoArea.setText(prescriptionDto_st.getPrescriptionDtoString());
    }

    //初始化
    public void initialize() {

    }


    public void findPatient(ActionEvent actionEvent) {
        prescriptionInfoArea.clear();
        patientInfoArea.clear();
        medicineArea.clear();
        Long patientId = Long.valueOf(patientIdFiled.getText());
        //根据patientId查找病人信息
        Patient byId = patientDAO.findById(patientId);
        if (byId == null) {
            patientInfoArea.setText("无法查找到该病人，请重新查找");
            return;
        }
        patient_st = byId;
        String patientInfo = "病历号：" + byId.getPatientId() +
                "\n病人姓名：" + byId.getPatientName() +
                "\n性别：" + byId.getSex() +
                "\n病房号：" + byId.getWardNo() +
                "\n床号：" + byId.getTheBedNo() +
                "\n病情：" + byId.getIllness();
        if(!byId.getDiagnose().equals("")){
            patientInfo += "\n诊断：" + byId.getDiagnose();
        }
        patientInfoArea.setText(patientInfo);
        Prescription byPatientId = prescriptionDAO.getByPatientId(patient_st.getPatientId());
        if(byPatientId == null){
            Prescription prescription1 = new Prescription();
            prescription1.setPatientId(patient_st.getPatientId());
            prescription1.setPatientName(patient_st.getPatientName());
            prescription1.setDoctorName(patient_st.getDoctorName());
            prescription1.setBedNo(patient_st.getTheBedNo());
            prescription1.setWardNo(patient_st.getWardNo());
            prescription1.setTheBedNo(patient_st.getTheBedNo());
            prescription1.setDoctorId(patient_st.getDoctorId());
            prescription1.setDeptId(patient_st.getDeptId());
            prescription1.setDeptName(patient_st.getDeptName());
            prescription1.setStatus(0);
            prescription1.setTotalPrice(new BigDecimal(0));
            prescriptionDAO.save(prescription1);
            byPatientId = prescription1;
        }
        prescription_st = byPatientId;
        prescriptionDto_st.setPrescription(byPatientId);
        prescriptionDto_st.setPrescriptionMedicineList(null);
        List<PrescriptionMedicine> prescriptionMedicineList = prescriptionMedicineDAO.listByPrescriptionId(byPatientId.getPrescriptionId());
        if(prescriptionMedicineList == null || prescriptionMedicineList.size() == 0){
            prescriptionInfoArea.setText(prescriptionDto_st.getPrescriptionDtoString());
            return;
        }
        prescriptionDto_st.setPrescriptionMedicineList(prescriptionMedicineList);
        prescriptionInfoArea.setText(prescriptionDto_st.getPrescriptionDtoString());
    }

    public void chooseMedicine(ActionEvent actionEvent) {
        String medicineName = medicineCombo.getValue();
        for(Medicine medicine : medicines_st){
            if(medicine.getMedicineName().equals(medicineName)){
                medicine_st = medicine;
                break;
            }
        }
        String medicineInfo = "药品编号：" + medicine_st.getMedicineId()+
                "\n药品名称：" + medicine_st.getMedicineName() +
                "\n药品价格：" + medicine_st.getPrice() ;
        medicineArea.clear();
        medicineArea.setText(medicineInfo);
    }
}
